23 template <class T
> string
toStr(const T
&x
){
24 stringstream s
; s
<< x
; return s
.str();
27 template <class T
> int toInt(const T
&x
){
28 stringstream s
; s
<< x
; int r
; s
>> r
; return r
;
31 #define For(i, a, b) for (int i=(a); i<(b); ++i)
32 #define foreach(x, v) for (typeof (v).begin() x = (v).begin(); x != (v).end(); ++x)
33 #define D(x) cout << #x " = " << (x) << endl
35 const double EPS
= 1e-9;
36 int cmp(double x
, double y
, double tol
= EPS
){
37 return (x
<= y
+ tol
) ? (x
+ tol
< y
) ? -1 : 0 : 1;
46 bool segundo_de_mayor_a_menor(pair
<int, int> a
, pair
<int, int> b
){
47 if (a
.second
!= b
.second
) return a
.second
> b
.second
;
48 return a
.first
< b
.first
;
52 pair
<int, int> f(int u
, int dad
= -1){
53 vector
< pair
<int, int> > p
;
54 for (int i
= 0; i
< g
[u
].size(); ++i
){
56 if (v
== dad
) continue;
58 p
.push_back( f(v
, u
) );
61 sort(p
.begin(), p
.end(), segundo_de_mayor_a_menor
);
64 int tengo
= A
[u
] - B
[u
];
65 for (int i
= 0; i
< p
.size(); ++i
){
66 if (tengo
> p
[i
].first
){
67 tengo
-= p
[i
].first
- p
[i
].second
;
69 total
+= p
[i
].first
- tengo
;
73 //printf(" f(u=%d) = <%d, %d>\n", u, total, tengo);
74 return make_pair(total
, tengo
);
81 cout
<< "Case " << Caso
++ << ": ";
83 for (int i
= 0; i
< n
; ++i
){
88 A
[i
] = max(A
[i
], B
[i
]);
93 for (int i
= 0; i
< n
- 1; ++i
){
101 //for (int i = 0; i < n; ++i){
102 // printf("Nodo %d: A = %d, B = %d\n", i, A[i], B[i]);
106 for (int i
= 0; i
< n
; ++i
){
108 pair
<int, int> p
= f(i
);
109 //printf("Empezando en %d: <%d, %d>\n", i, p.first, p.second);
112 ans
= min(ans
, p
.first
);